package com.example.demo3_drug.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo3_drug.common.ResultMapUtil;
import com.example.demo3_drug.entity.Supplier;
import com.example.demo3_drug.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService;

    @RequestMapping
    public String supplier(){
        return "/supplier";
    }


    @RequestMapping(value = "/supplierQueryPage")
    @ResponseBody
    public Object supplierQueryPage(String param , @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        try {
            IPage<Supplier> iPage = supplierService.selectSupplierPage(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "supplierPage")
    public String supplierPage(){
        return "/supplierPage";
    }


    /**
     * 新增一个供应商
     * @return
     */
    @RequestMapping(value = "/supplierAdd")
    @ResponseBody
    public Object supplierAdd(Supplier supplier){
        try {
            supplier.setCreatetime(new Date());
            int i =  supplierService.addSupplier(supplier);
            return ResultMapUtil.getHashMapSave(i);
        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/supplierQueryById")
    public String supplierQueryById(@RequestParam(name = "id", required = true) Integer id, Model model){

        Supplier supplier = supplierService.querySupplierById(id);
        model.addAttribute("obj",supplier);
        return "/supplierPage";
    }

    @RequestMapping(value = "/supplierEdit")
    @ResponseBody
    public Object supplierEdit(Supplier supplier){

        try {
            int i =  supplierService.editSupplier(supplier);
            return ResultMapUtil.getHashMapSave(i);
        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/supplierDelById")
    @ResponseBody
    public Object supplierDelById(Integer id){

        try {
            int i =  supplierService.deleteSupplier(id);
            return ResultMapUtil.getHashMapDel(i);
        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }



}
